package com.example.gustavo.hello.activity;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.gustavo.hello.R;
import com.example.gustavo.hello.listener.OnScrollHideHeaderListener;

import java.util.ArrayList;
import java.util.List;

/**
 * RecyclerView + Toolabar
 */
public class ToolbarActivity extends ActionBarActivity {

    private static final String TAG = "ToolbarActivity";
    private RecyclerView recyclerView;
    private Toolbar toolbar;
    private ImageButton ib_bottom;
    private ArrayList<String> list;
    private int distanceY;
    private boolean isShowing = true;
    private int distance = 40;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toolbar);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ib_bottom = (ImageButton) findViewById(R.id.ib_bottom);



        toolbar.setSaveEnabled(true);
        initToolbar();
        initRecyclerView();

        ib_bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                recyclerView.smoothScrollToPosition(list.size());



            }
        });
    }

    private void initRecyclerView() {
        list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {

            list.add("text "+i);
        }


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);

        recyclerView.setOnScrollListener(new OnScrollHideHeaderListener() {
            @Override
            public void onShow() {
                show();
            }

            @Override
            public void onHide() {
                hide();

            }
        });
    }

    private void show() {

        toolbar.animate().translationY(0).setInterpolator(new AccelerateInterpolator(2));

        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) ib_bottom.getLayoutParams();
        int bottomMargin = layoutParams.bottomMargin;
        ib_bottom.animate().translationY(-bottomMargin).setInterpolator(new DecelerateInterpolator(2)).start();
    }

    private void hide() {


        toolbar.animate().translationY(-toolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2));

        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) ib_bottom.getLayoutParams();
        int bottomMargin = layoutParams.bottomMargin;
        ib_bottom.animate().translationY(bottomMargin + ib_bottom.getHeight()).setInterpolator(new DecelerateInterpolator(2)).start();

    }

    private void initToolbar() {

        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("toolbar");

        toolbar.setNavigationIcon(R.drawable.ic_launcher);
        setSupportActionBar(toolbar);


        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    static class MyHolder extends RecyclerView.ViewHolder{
        TextView textView;

        public MyHolder(View itemView,boolean inflate) {
            super(itemView);

            if(inflate){

                textView = (TextView) itemView.findViewById(R.id.textView);
            }
        }



    }

    class MyAdapter extends RecyclerView.Adapter<MyHolder>{
        private static final int ITEM_HEAD = 1;
        private static final int ITEM_NORMAL = 2;


        @Override
        public MyHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

            if(viewType == ITEM_HEAD){

                return new MyHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.toolbar_head,viewGroup,false),false);
            }else{

                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_toolbar,viewGroup,false);
                return new MyHolder(view,true);
            }

        }

        @Override
        public void onBindViewHolder(MyHolder myHolder, int position) {

            if(position != 0){

                myHolder.textView.setText(list.get(position -1));
            }


        }

        @Override
        public int getItemCount() {
            return list.size() + 1;
        }

        @Override
        public int getItemViewType(int position) {

            if(position == 0){
               return ITEM_HEAD;
            }else{
                return ITEM_NORMAL;
            }

        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
